<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/14
 * Time: 17:47
 * 订单管理
 */
require('env.php');
require('init.inc.php');
$post_data = $_POST;
if ($post_data['isUpdate'] == '0_up') {
    if (!$post_data['sjr']) {
        exit(json_encode(array('msg' => 'false', 'content' => '收件人必填項，不能為空')));
    }

    if (!$post_data['phone']) {
        exit(json_encode(array('msg' => 'false', 'content' => '手機號必填項，不能為空')));
    }

    if (LANG == 'TW') {
        if (!preg_match("/^\d{10}$/", $post_data['phone'])) {
            exit(json_encode(array('msg' => 'false', 'content' => '非法的手機號碼')));
        }
    }

    if (!$post_data['addr']) {
        exit(json_encode(array('msg' => 'false', 'content' => '詳細地址必填項，不能為空')));
    }


    $addr = $post_data['city'] . $post_data['qx'] . $post_data['addr'];
    $db_oid = $post_data['oid'];
    $inp_data = array('userid' => $_COOKIE['ansyuserid'], 'user_name' => $post_data['sjr'], 'shipping_time' => $post_data['shdt'], 'phone' => $post_data['phone'], 'addres' => $addr, 'post_code' => '000000', 'modifytime' => time());
    $ret = Order::update($inp_data, $db_oid);
    if ($ret) {
        exit(json_encode(array('msg' => 'true', 'content' => '成功')));
    } else {
        exit(json_encode(array('msg' => 'false', 'content' => '服務器錯誤')));
    }
} elseif ($post_data['isUpdate'] == '0_add') {
    $lastId = Sn::lastedInsertId();
    $order_sn = "SN_" . $lastId;
    $inp_data = array('order_sn' => $order_sn, 'shipping_method' => $post_data['ysfs'], 'pay_type' => $post_data['fkfs'], 'createtime' => time(), 'modifytime' => time(), 'userid' => $_COOKIE['ansyuserid'], 'user_name' => '', 'shipping_time' => '', 'phone' => '', 'addres' => '', 'gender' => '', 'email' => '', 'note' => '');
    $ret = Order::add($inp_data);
    if ($ret) {
        exit(json_encode(array('msg' => 'true', 'id' => $ret, 'content' => '成功')));
    } else {
        exit(json_encode(array('msg' => 'false', 'content' => '服務器錯誤')));
    }
} elseif ($post_data['isUpdate'] == '0_finish') {
    if (!$post_data['email'] || !$post_data['line']) {
        exit(json_encode(array('msg' => 'false', 'content' => 'Email信箱和line必須填写，這是保障您權益的有效方式')));
    }

    if (!filter_var($post_data['email'], FILTER_VALIDATE_EMAIL)) {
        exit(json_encode(array('msg' => 'false', 'content' => 'Email信箱格式不正確，請重新輸入')));
    }
//
//    $success = Utils::check_email($post_data['email']);
//    if (!$success) {
//        exit(json_encode(['msg' => 'false', 'content' => '非法的郵箱地址']));
//    }

    $db_oid = $post_data['oid'];
    $inp_data = array('gender' => $post_data['gender'], 'email' => $post_data['email'], 'line' => $post_data['line'], 'note' => $post_data['note'], 'modifytime' => time(), 'status' => 0);//最後一步徹底完成訂單才算有效，所以更新status
    $ret = Order::update($inp_data, $db_oid);
    if ($ret) {
        //更新訂單產品表狀態，並且插入訂單號
        $order = Order::getById($db_oid);
        $up_data = array('iddel' => '-1', 'order_sn' => $order['order_sn'], 'modifytime' => time());

//        var_dump($ret);exit;
        $userid = $_COOKIE['ansyuserid'];
        $where = "WHERE userid ='$userid' AND iddel=1";
        $rt = OrderProduct::updateConditon($up_data, $where);
        exit(json_encode(array('msg' => 'true', 'fk_code' => $order['pay_type'], 'order_sn' => $order['order_sn'], 'content' => '成功')));
    } else {
        exit(json_encode(array('msg' => 'false', 'content' => '服務器錯誤')));
    }
} elseif ($post_data['isUpdate'] == '1') {
    $oid = $post_data['order_db_id'];
    $order = OrderProduct::getOrderById($oid);
    if (!$order) {
        exit(json_encode(array('msg' => 'false', 'content' => '不存在的訂單')));
    }
//合法话修改數據庫狀態
//    $update_data1 = array('iddel' => '-1');
    $ret = OrderProduct::delete($oid);
    if ($ret) {
        exit(json_encode(array('msg' => 'true', 'content' => '刪除成功')));
    } else {
        exit(json_encode(array('msg' => 'false', 'content' => '不存在的訂單')));
    }
} elseif ($post_data['isUpdate'] == 'all') {//清空購物車
    $ret = OrderProduct::delete();
    if ($ret) {
        exit(json_encode(array('msg' => 'true', 'content' => '清空成功')));
    } else {
        exit(json_encode(array('msg' => 'false', 'content' => '清空失敗')));
    }
} elseif ($post_data['isUpdate'] == '-1') {
    //判斷當前用戶購物車是否有物品

    $Oprods = OrderProduct::getAllBuyProds($_COOKIE['ansyuserid']);
    if ($Oprods) {
        exit(json_encode(array('msg' => 'true')));
    } else {
        exit(json_encode(array('msg' => 'false')));
    }
}


